<template><div><h3 id="_0-前言" tabindex="-1"><a class="header-anchor" href="#_0-前言" aria-hidden="true">#</a> 0 前言</h3>
<p><a href="http://www.dooccn.com/shell/" target="_blank" rel="noopener noreferrer">在线bash工具<ExternalLinkIcon/></a></p>
<h3 id="_1-验证输入" tabindex="-1"><a class="header-anchor" href="#_1-验证输入" aria-hidden="true">#</a> 1 验证输入</h3>
<p>​	编写shell脚本验证用户输入，仅限字母数字，不能包含标点符号、特殊字符和空格。</p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token shebang important">#!/bin/bash</span>
<span class="token keyword">function</span> <span class="token function-name function">validString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token comment"># 移除非数字和字母的字符</span>
	<span class="token assign-left variable">validchars</span><span class="token operator">=</span><span class="token string">"<span class="token variable"><span class="token variable">$(</span><span class="token builtin class-name">echo</span> $1 <span class="token operator">|</span><span class="token function">sed</span> <span class="token string">'s/[^[:alnum:]]//g'</span><span class="token variable">)</span></span>"</span>
	
	<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$validchars</span>"</span> <span class="token operator">=</span> <span class="token string">"<span class="token variable">$1</span>"</span> <span class="token punctuation">]</span>
		<span class="token keyword">then</span>
			<span class="token builtin class-name">return</span> <span class="token number">0</span>
		<span class="token keyword">else</span>
			<span class="token builtin class-name">return</span> <span class="token number">1</span>
	<span class="token keyword">fi</span>
<span class="token punctuation">}</span>

<span class="token builtin class-name">read</span> -p <span class="token string">"Enter input :"</span> input
<span class="token keyword">if</span> validString <span class="token variable">$input</span> 
    <span class="token keyword">then</span> 
        <span class="token builtin class-name">echo</span> <span class="token string">"Your input is valid!"</span>
    <span class="token keyword">else</span>
        <span class="token builtin class-name">echo</span> <span class="token string">"Your input is invalid!"</span>
        <span class="token builtin class-name">exit</span> <span class="token number">1</span>
<span class="token keyword">fi</span>
<span class="token builtin class-name">exit</span> <span class="token number">0</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="seg" tabindex="-1"><a class="header-anchor" href="#seg" aria-hidden="true">#</a> seg</h4>
<p><code v-pre>seg 's/pattern/replacement/g'</code> 将匹配到所有字符串替换为<code v-pre>replacement</code></p>
<ul>
<li><code v-pre>s</code> 表示search</li>
<li><code v-pre>pattern</code> 表示匹配模式</li>
<li><code v-pre>replacement</code> 替换选项</li>
<li><code v-pre>g</code> 全局匹配</li>
</ul>
<h4 id="函数调用" tabindex="-1"><a class="header-anchor" href="#函数调用" aria-hidden="true">#</a> 函数调用</h4>
<p><code v-pre>funcation $parameter</code></p>
<p>用于<code v-pre>if</code>的条件：返回值为0，表示true；返回值为其他，表示false；</p>
<h4 id="shell中的简写正则表达式" tabindex="-1"><a class="header-anchor" href="#shell中的简写正则表达式" aria-hidden="true">#</a> shell中的简写正则表达式</h4>
<ul>
<li><code v-pre>[:alpha:]</code> 匹配任意字母，大小写都有</li>
<li><code v-pre>[:alnum:]</code> 匹配任意字母和数字</li>
<li><code v-pre>[:blank:]</code> 匹配空格和制表符</li>
<li><code v-pre>[:digit:]</code> 匹配数字</li>
<li><code v-pre>[:lower:]</code> 匹配小写字母</li>
<li><code v-pre>[:upper:]</code> 匹配大写字母</li>
<li><code v-pre>[:print:]</code> 匹配可打印字符</li>
<li><code v-pre>[:punct:]</code> 匹配标点符号</li>
<li><code v-pre>[:space:]</code> 匹配任意空白字符：空格、制表符、<code v-pre>NL</code>、<code v-pre>FF</code>、<code v-pre>VT</code>、<code v-pre>CR</code></li>
</ul>
<h3 id="_2-规范月份输入" tabindex="-1"><a class="header-anchor" href="#_2-规范月份输入" aria-hidden="true">#</a> 2 规范月份输入</h3>
<p>​	编写一个shell脚本，接收月份字符串或者数字作为输入，输出长度为3的缩写形式。如：输入January，输出Jan。输入1，输出Jan。</p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token shebang important">#!/bin/bash</span>

<span class="token keyword">function</span> <span class="token function-name function">numToName</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token variable">$1</span> <span class="token keyword">in</span>
        <span class="token number">1</span><span class="token punctuation">)</span> <span class="token assign-left variable">month</span><span class="token operator">=</span><span class="token string">"Jan"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
        <span class="token number">2</span><span class="token punctuation">)</span> <span class="token assign-left variable">month</span><span class="token operator">=</span><span class="token string">"Feb"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
        <span class="token number">3</span><span class="token punctuation">)</span> <span class="token assign-left variable">month</span><span class="token operator">=</span><span class="token string">"Mar"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
        <span class="token number">4</span><span class="token punctuation">)</span> <span class="token assign-left variable">month</span><span class="token operator">=</span><span class="token string">"Apr"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
        <span class="token number">5</span><span class="token punctuation">)</span> <span class="token assign-left variable">month</span><span class="token operator">=</span><span class="token string">"May"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
        <span class="token number">6</span><span class="token punctuation">)</span> <span class="token assign-left variable">month</span><span class="token operator">=</span><span class="token string">"Jun"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
        <span class="token number">7</span><span class="token punctuation">)</span> <span class="token assign-left variable">month</span><span class="token operator">=</span><span class="token string">"Jul"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
        <span class="token number">8</span><span class="token punctuation">)</span> <span class="token assign-left variable">month</span><span class="token operator">=</span><span class="token string">"Aug"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
        <span class="token number">9</span><span class="token punctuation">)</span> <span class="token assign-left variable">month</span><span class="token operator">=</span><span class="token string">"Sep"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
        <span class="token number">10</span><span class="token punctuation">)</span> <span class="token assign-left variable">month</span><span class="token operator">=</span><span class="token string">"Oct"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
        <span class="token number">11</span><span class="token punctuation">)</span> <span class="token assign-left variable">month</span><span class="token operator">=</span><span class="token string">"Nov"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
        <span class="token number">12</span><span class="token punctuation">)</span> <span class="token assign-left variable">month</span><span class="token operator">=</span><span class="token string">"Dec"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
        *<span class="token punctuation">)</span> 
            <span class="token builtin class-name">echo</span> <span class="token string">"Unknow month value!"</span>
            <span class="token builtin class-name">exit</span> <span class="token number">1</span>
    <span class="token keyword">esac</span>
    <span class="token builtin class-name">return</span> <span class="token number">0</span>
<span class="token punctuation">}</span>

<span class="token builtin class-name">read</span> -p <span class="token string">"Enter month:"</span> var

<span class="token keyword">if</span> <span class="token punctuation">[</span> -z <span class="token variable"><span class="token variable">$(</span><span class="token builtin class-name">echo</span> $var <span class="token operator">|</span> <span class="token function">sed</span> <span class="token string">'s/[[:digit:]]//g'</span><span class="token variable">)</span></span> <span class="token punctuation">]</span>
<span class="token keyword">then</span>
    numToName <span class="token variable">$var</span>
<span class="token keyword">else</span> 
    <span class="token assign-left variable">month</span><span class="token operator">=</span><span class="token string">"<span class="token variable"><span class="token variable">$(</span><span class="token builtin class-name">echo</span> $var <span class="token operator">|</span> <span class="token function">cut</span> -c1 <span class="token operator">|</span> <span class="token function">tr</span> <span class="token string">'[:lower:]'</span> <span class="token string">'[:upper:]'</span><span class="token variable">)</span></span>"</span>
    <span class="token assign-left variable">month</span><span class="token operator">=</span><span class="token string">"<span class="token variable">$month</span><span class="token variable"><span class="token variable">$(</span><span class="token builtin class-name">echo</span> $var <span class="token operator">|</span> <span class="token function">cut</span> -c2-3 <span class="token operator">|</span> <span class="token function">tr</span> <span class="token string">'[:upper:]'</span> <span class="token string">'[:lower:]'</span><span class="token variable">)</span></span>"</span>
<span class="token keyword">fi</span>
<span class="token builtin class-name">echo</span> <span class="token string">"<span class="token variable">$month</span>"</span>
<span class="token builtin class-name">exit</span> <span class="token number">0</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="_3-美化数字" tabindex="-1"><a class="header-anchor" href="#_3-美化数字" aria-hidden="true">#</a> 3 美化数字</h3>
<p>​	编写脚本，将输入的数字每三位加一个<code v-pre>,</code>，且要求千分位分隔符和小数分隔符可定制化。如：输入<code v-pre>1234</code>，输出<code v-pre>1,234</code></p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token shebang important">#!/bin/bash</span>

<span class="token keyword">function</span> <span class="token function-name function">beautifyNumber</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token assign-left variable">integer</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span><span class="token builtin class-name">echo</span> $1 <span class="token operator">|</span> <span class="token function">cut</span> -d. -f1<span class="token variable">)</span></span>
    <span class="token assign-left variable">decimal</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span><span class="token builtin class-name">echo</span> $1 <span class="token operator">|</span> <span class="token function">cut</span> -d. -f2<span class="token variable">)</span></span>

    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$decimal</span>"</span> <span class="token operator">!=</span> <span class="token string">"<span class="token variable">$1</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token assign-left variable">result</span><span class="token operator">=</span><span class="token string">"<span class="token variable">${DD<span class="token operator">:=</span>'.'}</span><span class="token variable">$decimal</span>"</span>
    <span class="token keyword">fi</span>

   <span class="token assign-left variable">thousand</span><span class="token operator">=</span><span class="token variable">$integer</span>

    <span class="token keyword">while</span> <span class="token punctuation">[</span> <span class="token variable">$thousand</span> -gt <span class="token number">999</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
            <span class="token assign-left variable">remainder</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$((</span>$thousand <span class="token operator">%</span> <span class="token number">1000</span><span class="token variable">))</span></span>

        <span class="token keyword">while</span> <span class="token punctuation">[</span> <span class="token variable">${<span class="token operator">#</span>remainder}</span> -lt <span class="token number">3</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">do</span>
            <span class="token assign-left variable">remainder</span><span class="token operator">=</span><span class="token string">"0<span class="token variable">$remainder</span>"</span>
        <span class="token keyword">done</span>

        <span class="token assign-left variable">result</span><span class="token operator">=</span><span class="token string">"${TD:="</span>,<span class="token string">"}<span class="token variable">${remainder}</span><span class="token variable">${result}</span>"</span>
        <span class="token assign-left variable">thousand</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$((</span>$thousand <span class="token operator">/</span> <span class="token number">1000</span><span class="token variable">))</span></span>
    <span class="token keyword">done</span>

    <span class="token assign-left variable">beautyNumber</span><span class="token operator">=</span><span class="token string">"<span class="token variable">$thousand</span><span class="token variable">$result</span>"</span>
    <span class="token builtin class-name">echo</span> <span class="token variable">$beautyNumber</span>
<span class="token punctuation">}</span>

<span class="token assign-left variable">DD</span><span class="token operator">=</span><span class="token string">"."</span>
<span class="token assign-left variable">TD</span><span class="token operator">=</span><span class="token string">","</span>

<span class="token keyword">while</span> <span class="token builtin class-name">getopts</span> <span class="token string">"d:t:"</span> opt<span class="token punctuation">;</span> <span class="token keyword">do</span>
    <span class="token keyword">case</span> <span class="token variable">$opt</span> <span class="token keyword">in</span>
        d<span class="token punctuation">)</span> <span class="token assign-left variable">DD</span><span class="token operator">=</span><span class="token string">"<span class="token variable">$OPTARG</span>"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
        t<span class="token punctuation">)</span> <span class="token assign-left variable">TD</span><span class="token operator">=</span><span class="token string">"<span class="token variable">$OPTARG</span>"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
    <span class="token keyword">esac</span>
<span class="token keyword">done</span>

<span class="token builtin class-name">shift</span> <span class="token variable"><span class="token variable">$((</span>$OPTIND<span class="token operator">-</span><span class="token number">1</span><span class="token variable">))</span></span>

beautifyNumber <span class="token variable">$1</span>

<span class="token builtin class-name">exit</span> <span class="token number">0</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="脚本读取参数" tabindex="-1"><a class="header-anchor" href="#脚本读取参数" aria-hidden="true">#</a> 脚本读取参数</h4>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token keyword">while</span> <span class="token builtin class-name">getopts</span> <span class="token string">"d:t:"</span> opt<span class="token punctuation">;</span> <span class="token keyword">do</span>
    <span class="token keyword">case</span> <span class="token variable">$opt</span> <span class="token keyword">in</span>
        d<span class="token punctuation">)</span> <span class="token assign-left variable">DD</span><span class="token operator">=</span><span class="token string">"<span class="token variable">$OPTARG</span>"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
        t<span class="token punctuation">)</span> <span class="token assign-left variable">TD</span><span class="token operator">=</span><span class="token string">"<span class="token variable">$OPTARG</span>"</span> <span class="token punctuation">;</span><span class="token punctuation">;</span>
    <span class="token keyword">esac</span>
<span class="token keyword">done</span>

<span class="token builtin class-name">shift</span> <span class="token variable"><span class="token variable">$((</span>$OPTIND<span class="token operator">-</span><span class="token number">1</span><span class="token variable">))</span></span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="custom-container warning"><p class="custom-container-title">警告</p>
<ul>
<li><code v-pre>$OPTIND</code> 是原始<code v-pre>$*</code>中下一个要处理的元素位置</li>
<li><code v-pre>shift</code> 用于移动参数</li>
<li><code v-pre>shift $(($OPTIND-1))</code> 相当于将<code v-pre>-d</code> 、<code v-pre>-t</code>这些参数覆盖掉，重新编号其余参数，即在下文<code v-pre>$1</code>就不会受到<code v-pre>-d</code>、<code v-pre>-t</code>的影响。</li>
</ul>
</div>
<h3 id="_4-验证整数输入" tabindex="-1"><a class="header-anchor" href="#_4-验证整数输入" aria-hidden="true">#</a> 4 验证整数输入</h3>
<p>​	编写脚本验证整数输入是否在指定区间范围内，其中整数可以为负数。</p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token shebang important">#!/bin/bash</span>

<span class="token keyword">function</span> <span class="token function-name function">checkInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token assign-left variable">number</span><span class="token operator">=</span><span class="token variable">$1</span>
    <span class="token assign-left variable">min</span><span class="token operator">=</span><span class="token variable">$2</span>
    <span class="token assign-left variable">max</span><span class="token operator">=</span><span class="token variable">$3</span>

    <span class="token keyword">if</span> <span class="token punctuation">[</span> -z <span class="token variable">$number</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token builtin class-name">echo</span> <span class="token string">"Input number can not be null!"</span>
        <span class="token builtin class-name">return</span> <span class="token number">1</span>
    <span class="token keyword">fi</span>

	<span class="token comment"># 处理方法绝妙</span>
    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">${number<span class="token operator">%</span>${number<span class="token operator">#</span>?}</span><span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token string">"-"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token assign-left variable">testValue</span><span class="token operator">=</span><span class="token variable">${number<span class="token operator">#</span>?}</span>
    <span class="token keyword">else</span>
        <span class="token assign-left variable">testValue</span><span class="token operator">=</span><span class="token variable">$number</span>
    <span class="token keyword">fi</span>

    <span class="token assign-left variable">nodights</span><span class="token operator">=</span><span class="token string">"<span class="token variable"><span class="token variable">$(</span><span class="token builtin class-name">echo</span> $testValue <span class="token operator">|</span> <span class="token function">sed</span> <span class="token string">'s/[[:digit:]]//g'</span><span class="token variable">)</span></span>"</span>

    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator">!</span> -z <span class="token variable">$nodights</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token builtin class-name">echo</span> <span class="token string">"Input is not a integer!"</span>
        <span class="token builtin class-name">return</span> <span class="token number">1</span>
    <span class="token keyword">fi</span>

    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator">!</span> -z <span class="token variable">$min</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$number</span>"</span> -lt <span class="token string">"<span class="token variable">$min</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
            <span class="token builtin class-name">echo</span> <span class="token string">"Input number is less than min!"</span>
            <span class="token builtin class-name">return</span> <span class="token number">1</span>
        <span class="token keyword">fi</span>
    <span class="token keyword">fi</span>

    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator">!</span> -z <span class="token variable">$max</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$number</span>"</span> -gt <span class="token string">"<span class="token variable">$max</span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
            <span class="token builtin class-name">echo</span> <span class="token string">"Input number is greater than max!"</span>
            <span class="token builtin class-name">return</span> <span class="token number">1</span>
        <span class="token keyword">fi</span>
    <span class="token keyword">fi</span>

    <span class="token builtin class-name">return</span> <span class="token number">0</span>
<span class="token punctuation">}</span>

<span class="token keyword">if</span> checkInt <span class="token string">"<span class="token variable">$1</span>"</span> <span class="token string">"<span class="token variable">$2</span>"</span> <span class="token string">"<span class="token variable">$3</span>"</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
    <span class="token builtin class-name">echo</span> <span class="token string">"Input number is valid!"</span>
<span class="token keyword">fi</span>

<span class="token builtin class-name">exit</span> <span class="token number">0</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="_5-验证浮点数输入" tabindex="-1"><a class="header-anchor" href="#_5-验证浮点数输入" aria-hidden="true">#</a> 5 验证浮点数输入</h3>
<p>​	编写脚本验证输入是浮点数，整数也属于浮点数。</p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code><span class="token keyword">function</span> <span class="token function-name function">checkInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token assign-left variable">number</span><span class="token operator">=</span><span class="token variable">$1</span>
    <span class="token assign-left variable">min</span><span class="token operator">=</span><span class="token variable">$2</span>
    <span class="token assign-left variable">max</span><span class="token operator">=</span><span class="token variable">$3</span>

    <span class="token keyword">if</span> <span class="token punctuation">[</span> -z <span class="token variable">$number</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token builtin class-name">echo</span> <span class="token string">"Input can not be null!"</span>
        <span class="token builtin class-name">return</span> <span class="token number">1</span>
    <span class="token keyword">fi</span>

    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">${number<span class="token operator">%</span>${number<span class="token operator">#</span>?}</span><span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token string">"-"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token assign-left variable">intValue</span><span class="token operator">=</span><span class="token variable">${number<span class="token operator">#</span>?}</span>
    <span class="token keyword">else</span>
        <span class="token assign-left variable">intValue</span><span class="token operator">=</span><span class="token variable">$number</span>
    <span class="token keyword">fi</span>

    <span class="token assign-left variable">nodigit</span><span class="token operator">=</span><span class="token string">"<span class="token variable"><span class="token variable">$(</span><span class="token builtin class-name">echo</span> $intValue <span class="token operator">|</span> <span class="token function">sed</span> <span class="token string">'s/[[:digit:]]//g'</span><span class="token variable">)</span></span>"</span>

    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator">!</span> -z <span class="token variable">$nodigit</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token builtin class-name">echo</span> <span class="token string">"Input is not integer!"</span>
        <span class="token builtin class-name">return</span> <span class="token number">1</span>
    <span class="token keyword">fi</span>

    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator">!</span> -z <span class="token variable">$min</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$number</span> -lt <span class="token variable">$min</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
            <span class="token builtin class-name">echo</span> <span class="token string">"Input is less than min!"</span>
            <span class="token builtin class-name">return</span> <span class="token number">1</span>
        <span class="token keyword">fi</span>
    <span class="token keyword">fi</span>

    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator">!</span> -z <span class="token variable">$max</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$number</span> -gt <span class="token variable">$max</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
            <span class="token builtin class-name">echo</span> <span class="token string">"Input is greater than max!"</span>
            <span class="token builtin class-name">return</span> <span class="token number">1</span>
        <span class="token keyword">fi</span>
    <span class="token keyword">fi</span>

    <span class="token builtin class-name">return</span> <span class="token number">0</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function-name function">checkFloat</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token assign-left variable">number</span><span class="token operator">=</span><span class="token variable">$1</span>

    <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator">!</span> -z <span class="token string">"<span class="token variable"><span class="token variable">$(</span><span class="token builtin class-name">echo</span> $number <span class="token operator">|</span> <span class="token function">sed</span> <span class="token string">'s/[^.]//g'</span><span class="token variable">)</span></span>"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
        <span class="token assign-left variable">former</span><span class="token operator">=</span><span class="token string">"<span class="token variable"><span class="token variable">$(</span><span class="token builtin class-name">echo</span> $number <span class="token operator">|</span> <span class="token function">cut</span> -d. -f1<span class="token variable">)</span></span>"</span>
        
        <span class="token comment"># 取第一个点之后的部分，与下边的逻辑连起来，可以防止出现1.2.1这种情况</span>
        <span class="token assign-left variable">latter</span><span class="token operator">=</span><span class="token variable">${number<span class="token operator">#</span>*\.}</span>

        <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator">!</span> -z <span class="token variable">$former</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
            <span class="token keyword">if</span> <span class="token operator">!</span> checkInt <span class="token variable">$former</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
                <span class="token builtin class-name">echo</span> <span class="token string">"Former of Input is not integer!"</span>
                <span class="token builtin class-name">return</span> <span class="token number">1</span>
            <span class="token keyword">fi</span>
        <span class="token keyword">fi</span>

        <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token operator">!</span> -z <span class="token variable">$latter</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
            <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">${latter<span class="token operator">%</span>${latter<span class="token operator">#</span>?}</span><span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token string">"-"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
                <span class="token builtin class-name">echo</span> <span class="token string">"Latter of Input is invalid!"</span>
                <span class="token builtin class-name">return</span> <span class="token number">1</span>
            <span class="token keyword">elif</span> <span class="token operator">!</span> checkInt <span class="token variable">$latter</span> <span class="token string">"0"</span> <span class="token string">""</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
                <span class="token builtin class-name">echo</span> <span class="token string">"Latter of Input is invalid!"</span>
                <span class="token builtin class-name">return</span> <span class="token number">1</span>
            <span class="token keyword">fi</span>
        <span class="token keyword">fi</span>
    <span class="token keyword">else</span>
        <span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token string">"<span class="token variable">$number</span>"</span> <span class="token operator">=</span> <span class="token string">"-"</span> <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
            <span class="token builtin class-name">echo</span> <span class="token string">"Input is invalid!"</span>
            <span class="token builtin class-name">return</span> <span class="token number">1</span>
        <span class="token keyword">fi</span>

        <span class="token keyword">if</span> <span class="token operator">!</span> checkInt <span class="token string">"<span class="token variable">$number</span>"</span> <span class="token string">""</span> <span class="token string">""</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
            <span class="token builtin class-name">echo</span> <span class="token string">"Input is not a float!"</span>
            <span class="token builtin class-name">return</span> <span class="token number">1</span>
        <span class="token keyword">fi</span>
    <span class="token keyword">fi</span>

    <span class="token builtin class-name">return</span> <span class="token number">0</span>
<span class="token punctuation">}</span>


<span class="token keyword">if</span> checkFloat <span class="token string">"<span class="token variable">$1</span>"</span><span class="token punctuation">;</span> <span class="token keyword">then</span>
    <span class="token builtin class-name">echo</span> <span class="token string">"Input is valid!"</span>
<span class="token keyword">fi</span>

<span class="token builtin class-name">exit</span> <span class="token number">0</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="_6-验证日期格式" tabindex="-1"><a class="header-anchor" href="#_6-验证日期格式" aria-hidden="true">#</a> 6 验证日期格式</h3>
<p>​	编写脚本验证输入日期是否真实存在。</p>
<div class="language-bash ext-sh line-numbers-mode"><pre v-pre class="language-bash"><code></code></pre><div class="line-numbers" aria-hidden="true"></div></div></div></template>
